package com.zhao.integration.test;

import java.io.File;

import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.PollableChannel;

import com.zhao.integration.DefaultMessagingGateway;
import com.zhao.test.UnitTesting;

public class ChannelRunnerTesting extends UnitTesting {

	@Autowired
	ConfigurableApplicationContext context;

	@Autowired
	MessageChannel inputChannel;

	@Autowired
	PollableChannel outputChannel;

	@Autowired
	MessageChannel ftpOutChannel;

	@Autowired
	PollableChannel ftpInChannel;

	@Autowired
	DefaultMessagingGateway defaultMessagingGateway;

	@Test
	public void contextLoads() {

	}

	@Test
	public void testMessageChannelByAnnotation() {
		inputChannel.send(MessageBuilder.withPayload("Hello World").build());
		Message<?> message = outputChannel.receive(100);
		logger.debug(message.toString());
	}

	@Test
	public void testMessageChannelByXml() {
		MessageChannel inputChannel = context.getBean("inputChannel", MessageChannel.class);
		PollableChannel outputChannel = context.getBean("outputChannel", PollableChannel.class);
		inputChannel.send(MessageBuilder.withPayload("Hello World").build());
		Message<?> message = outputChannel.receive(100);
		logger.debug(message.toString());
	}

	@Test
	public void testMessageChannelByFtp() {
		try {
			final File fileToSendA = new File("d://bms.log");
			final Message<File> messageA = MessageBuilder.withPayload(fileToSendA).build();
			ftpOutChannel.send(messageA);
			Thread.sleep(2000);

			Message<?> message = ftpInChannel.receive(2000);
			logger.info(String.format("Received first file message: %s.", message));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Test
	public void testPublishSubscribeChannel() {
		try {
			defaultMessagingGateway.publish("abc");
			defaultMessagingGateway.asyncPublish("123");

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
